﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace myTimesheet
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            cboProjects.DisplayMember = "Title";
            cboProjects.ValueMember = "GUID";

            cboProjects.DataSource = new Projects().GetProjects().Tables[0];

            this.cboProjects.SelectedIndexChanged += new System.EventHandler(this.cboProjects_SelectedIndexChanged);

            cboProjects.SelectedValue = new Projects().GetDefault();

            btnStatus.Text = "Start";
            btnStatus.ForeColor = Color.Green;


            ReloadProjectDetails();


        }

        private void btnProject_Click(object sender, EventArgs e)
        {
            new frmAdd().ShowDialog();
            cboProjects.DataSource = new Projects().GetProjects().Tables[0];
        }

        private void cboProjects_SelectedIndexChanged(object sender, EventArgs e)
        {
            new Projects().SetDefault(cboProjects.SelectedValue.ToString());
            ReloadProjectDetails();
            

        }

        private void ReloadProjectDetails() 
        {
            DataRow drProject = new Projects().GetProjectDetails(cboProjects.SelectedValue.ToString()).Tables[0].Rows[0];

            if (drProject["PaidDate"].ToString().Length > 0)
                ctlPaidDate.Value = Convert.ToDateTime(drProject["PaidDate"]);
        }

        string _GuidInUse = "";

        private void btnStatus_Click(object sender, EventArgs e)
        {
            if (btnStatus.Text == "Start")
            {
                _GuidInUse=new Projects().SetStart(cboProjects.SelectedValue.ToString());
                cboProjects.Enabled = false;
                btnStatus.Text = "Stop";
                btnStatus.ForeColor = Color.Red;
            }
            else 
            {
                cboProjects.Enabled = true;
                new Projects().SetEnd(_GuidInUse);
                btnStatus.Text = "Start";
                btnStatus.ForeColor = Color.Green;
            }
        }

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason != CloseReason.ApplicationExitCall)
            {
                Hide();
                e.Cancel = true;
            }
        }

        private void tmrMain_Tick(object sender, EventArgs e)
        {
            DateTime _Now = DateTime.Now;
            lblDate.Text = _Now.ToShortDateString();
            lblTime.Text = _Now.ToShortTimeString();
        }

        private void mnuExit_Click(object sender, EventArgs e)
        {
            if (btnStatus.Text=="Stop")
                new Projects().SetEnd(_GuidInUse);

            Application.Exit();
        }

        private void mnuMyTimesheet_Click(object sender, EventArgs e)
        {
            Show();
        }

        private void nftMain_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            Show();
        }

        private void btnReport_Click(object sender, EventArgs e)
        {
            if (printDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                pd.PrinterSettings = printDialog.PrinterSettings;
                pd.Print();
            }
        }

        int _top = 0;
        TimeSpan allProjectTime = new TimeSpan();

        private void pd_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            _top = 0;
            Graphics g = e.Graphics;

            // Load Data
            DataSet ds = new Projects().GetProjects();

            allProjectTime = new TimeSpan();

            if (ctlAllProjects.Checked)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    _top += 50;
                    AddProject(g, e.PageBounds, dr["Title"].ToString(), new Projects().GetTime(dr["GUID"].ToString()), ctlPaidDate.Value);
                }
            }
            else
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    if (cboProjects.SelectedValue.ToString()==dr["GUID"].ToString())
                    {
                        _top += 50;
                        AddProject(g, e.PageBounds, dr["Title"].ToString(), new Projects().GetTime(dr["GUID"].ToString()), Convert.ToDateTime(dr["PaidDate"]));  
                    }
                    
                }

            
            }

            _top += 20;

            g.DrawLine(new Pen(Brushes.Black, 3), new Point(10, _top + 35), new Point(e.PageBounds.Width - 10, _top + 35));
            _top += 40;

            g.DrawString(string.Format("{0}:{1}:{2}:{3}",allProjectTime.Days.ToString(), allProjectTime.Hours.ToString()
                   , allProjectTime.Minutes.ToString(), allProjectTime.Seconds.ToString()), new Font("Tahoma", 16), Brushes.Black, new PointF(430, _top));
            
        }

        private void AddProject(Graphics g, Rectangle f, string text, DataSet Data, DateTime PaidDate) 
        {
            Pen p2 = new Pen(Brushes.Black, 3);
            Font font = new System.Drawing.Font("Tahoma", 11);

            g.DrawString(text, new Font("Tahoma", 19), Brushes.Black, new PointF(10, _top));
            g.DrawLine(p2, new Point(10, _top + 35), new Point(f.Width-10, _top + 35));

            _top += 50;

            g.DrawString("Start", font, Brushes.Black, new PointF(30, _top));
            g.DrawString("End", font, Brushes.Black, new PointF(230, _top));

            g.DrawString("Days:Hours:Minutes:Seconds", font, Brushes.Black, new PointF(430, _top));

            g.DrawString("Comment", font, Brushes.Black, new PointF(650, _top));


            TimeSpan total = new TimeSpan();

            foreach (DataRow dr in Data.Tables[0].Rows)
            {
                _top += 20;

                g.DrawString(dr["Start"].ToString(), font, Brushes.Black, new PointF(30, _top));
                g.DrawString(dr["End"].ToString(), font, Brushes.Black, new PointF(230, _top));

                TimeSpan t = TimeSpan.Zero;
                if (Convert.ToDateTime(dr["Start"]) > PaidDate)
                {
                    t = Convert.ToDateTime(dr["End"]) - Convert.ToDateTime(dr["Start"]);


                }
                else {
                    g.DrawString("Paid", font, Brushes.Black, new PointF(650, _top));
                
                }

                total += t;

                g.DrawString(string.Format("{0}:{1}:{2}:{3}",t.Days.ToString(), t.Hours.ToString()
                    , t.Minutes.ToString(), t.Seconds.ToString()), font, Brushes.Black, new PointF(430, _top));
                
            }

            _top += 30;

            g.DrawString(string.Format("{0}:{1}:{2}:{3}          Hours: {4}", new object[]{total.Days.ToString(), total.Hours.ToString()
                   , total.Minutes.ToString(), total.Seconds.ToString(), Math.Round(total.TotalHours, 1).ToString()}), font, Brushes.Black, new PointF(430, _top));

            allProjectTime += total;

        }

        private void btnPreview_Click(object sender, EventArgs e)
        {
            ppd.ShowDialog();
        }

        private void btnPdf_Click(object sender, EventArgs e)
        {

        }

        private void ctlPaidDate_ValueChanged(object sender, EventArgs e)
        {
            new Projects().SetProjectDetails(cboProjects.SelectedValue.ToString(), ctlPaidDate.Value);

        }
    }
}
